package tiago.textToSpeech;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import java.util.Locale;
import java.util.Random;

public class TextToSpeechActivity extends Activity implements
		TextToSpeech.OnInitListener , OnClickListener
{
	private static final String TAG = "TextToSpeechDemo";

	private TextToSpeech mTts;
	private Button btnSpeech;
	private TextView txtViewContentToBeSpeeched;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// Initialize text-to-speech. This is an asynchronous operation.
		// The OnInitListener (second argument) is called after initialization
		// completes.
		mTts = new TextToSpeech(this, this // TextToSpeech.OnInitListener
		);

		// The button is disabled in the layout.
		// It will be enabled upon initialization of the TTS engine.
		btnSpeech = (Button) findViewById(R.id.again_button);

		btnSpeech.setOnClickListener(this);

		txtViewContentToBeSpeeched = (TextView) findViewById(R.id.editText1);
	}

	@Override
	public void onDestroy()
	{
		// Don't forget to shutdown!
		if (mTts != null)
		{
			mTts.stop();
			mTts.shutdown();
		}

		super.onDestroy();
	}

	// Implements TextToSpeech.OnInitListener.
	public void onInit(int status)
	{
		// status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
		if (status == TextToSpeech.SUCCESS)
		{
			// Set preferred language to US english.
			// Note that a language may not be available, and the result will
			// indicate this.
			int result = mTts.setLanguage(Locale.US);
			// Try this someday for some interesting results.
			// int result mTts.setLanguage(Locale.FRANCE);
			if (result == TextToSpeech.LANG_MISSING_DATA
					|| result == TextToSpeech.LANG_NOT_SUPPORTED)
			{
				// Lanuage data is missing or the language is not supported.
				Log.e(TAG, "Language is not available.");
			} else
			{
				// Check the documentation for other possible result codes.
				// For example, the language may be available for the locale,
				// but not for the specified country and variant.

				// The TTS engine has been successfully initialized.
				// Allow the user to press the button for the app to speak
				// again.
				btnSpeech.setEnabled(true);
			}
		} else
		{
			// Initialization failed.
			Log.e(TAG, "Could not initialize TextToSpeech.");
		}
	}
	

	/**
	 * Function that get the content of text typed by user and put to speech.
	 */
	private void sayHello()
	{
		String hello = txtViewContentToBeSpeeched.getText().toString();
		
		mTts.speak(hello, TextToSpeech.QUEUE_FLUSH, // Drop all pending entries
				// in the playback queue.
				null);
	}

	@Override
	public void onClick(View v)
	{
		if(v == this.btnSpeech)
		{
			sayHello();
		}
	}
}